<?php

namespace app\admin\controller;

use app\admin\model\Link as LinkModel;
use think\Request;

class LinkController extends BaseController
{
    public function lst()
    {
        $params = $this->request->param();
        if(!empty($params['name'])){
            $name = "%".$params['name']."%";
        }else{
            $name = "%%";
        }

        $data = LinkModel::where('name|link','like',$name)
            ->order('id', 'asc')->paginate(6,false,['query'=>\request()->param()]);
      //  var_dump($data);die;
        $page = $data->render();
        $this->assign([
            'page' => $page,
            'list' => $data,
        ]);
        return $this->fetch();
    }

    public function add()
    {
        $request = Request::instance();
        switch  ($request->method()){
            case "GET":
                return $this->fetch();
                break;
            case "POST":
                $params = $request->param();
                $controller = $this->getLowerController();
                if(request()->file('img')){
                    $picInfo = uploadOne($controller,'thumb','img',80,60);
                    $params['img_url'] = $picInfo['pic'];
                    $params['thumb_url'] = $picInfo['thumb_pic'];
                }
                //var_dump($params);die;
                $model = new LinkModel();
                if($model->allowField(true)->save($params)){
                    $this->success('添加友情链接成功!',url('admin/link/lst'),'',1);
                    exit;
                }
                else{
                    $this->error($model->getError());
                    exit;
                }
                break;
            default:
                return "错误的数据信息";
        }
    }

    public function edi()
    {
        $request = Request::instance();
        switch  ($request->method()){
            case "GET":
                $id = $request->param('id');
                $this->assign('data',LinkModel::find($id));
                return $this->fetch();
                break;
            case "POST":
                $params = $request->param();
                $model = new LinkModel();
                if(request()->file('img')){
                    $oldPic = $model->where('id','eq',$params['id'])->find();
                    if(is_file($oldPic['img_url'])){
                        unlink($oldPic['img_url']);
                    }
                    if(is_file($oldPic['thumb_url'])){
                        unlink($oldPic['thumb_url']);
                    }
                    $controller = strtolower($request->controller());
                    $picInfo = uploadOne($controller,'thumb','img',80,60);
                    $params['img_url'] = $picInfo['pic'];
                    $params['thumb_url'] = $picInfo['thumb_pic'];
                }
                if($model->allowField(true)->isUpdate(true)->save($params)){
                    $this->success('修改友情链接成功!',url('admin/link/lst'),'',1);
                    exit;
                }
                else{
                    $this->error($model->getError());
                    exit;
                }
                break;
            default:
                return "错误的数据信息";
        }
    }

    public function del()
    {
        $id = $this->request->param("id");
        $model = LinkModel::find($id);
        if(is_file($model->img_url)){
            unlink($model->img_url);
        }
        if(is_file($model->thumb_url)){
            unlink($model->thumb_url);
        }
        if(LinkModel::destroy($id)){
            return '亲,您已删除成功!';
        }
        return '删除失败,发生了什么^O^';
    }

}
